package com.example.girl.activity;

import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.girl.fragment.GirlsFragment;
import com.example.girl.util.SpfUtils;
import com.example.lbstest.R;
import com.king.base.BaseActivity;
import com.king.base.adapter.ViewPagerFragmentAdapter;
import com.king.base.model.EventMessage;
import com.king.base.util.SystemUtils;

import com.king.widget.SuperSlidingPaneLayout;

import java.util.ArrayList;
import java.util.List;

public class PhotoActivity extends BaseActivity implements View.OnClickListener {

    private TextView tvVersion;

    private SuperSlidingPaneLayout superSlidingPaneLayout;

    private Toolbar toolbar;

    private TabLayout tabLayout;

    private ViewPager viewPager;

    private FloatingActionButton fab;

    private ViewPagerFragmentAdapter viewPagerFragmentAdapter;

    private List<Fragment> listData;

    private List<CharSequence> listTabs;

    private OnScrollListener onScrollListener;

    public interface OnScrollListener {
         void onScroll();
    }


    @Override
    public void initUI() {

        setContentView(R.layout.activity_main2);

        tvVersion = findView(R.id.tvVersion);

        tvVersion.setText(String.format("V%s", SystemUtils.getVersionName(context)));

        superSlidingPaneLayout = findView(R.id.superSlidingPaneLayout);

        toolbar = findView(R.id.toolBar);
        setSupportActionBar(toolbar);
        tabLayout = findView(R.id.tabLayout);

        viewPager = findView(R.id.viewPager);

        fab = findView(R.id.fab);
        card = findView(R.id.card);
        fankui = findView(R.id.fankui);
    }
    CardView card,fankui;
    @Override
    public void addListeners() {
        fab.setOnClickListener(this);
        card.setOnClickListener(this);
        fankui.setOnClickListener(this);

    }



    @Override
    public void initData() {


        listData = new ArrayList<>();

        listTabs = new ArrayList<>();

        for (int i = 0; i < 5; i++) {

            switch (i){
                case 0:
                    listTabs.add("景点推荐");
//                    listData.add(GirlsFragment.newInstance(GirlsFragment.LayoutType.StaggeredGridLayout));
//                    onScrollListener = (OnScrollListener) listData.get(0);
                    listData.add(GirlsFragment.newInstance(GirlsFragment.LayoutType.StaggeredGridLayout,"景点推荐"));
                    break;
                case 1:
                    listTabs.add("餐馆推荐" );
                    listData.add(GirlsFragment.newInstance(GirlsFragment.LayoutType.StaggeredGridLayout,"餐馆推荐"));
                    break;
                case 2:
                    listTabs.add("酒店推荐" );
                    listData.add(GirlsFragment.newInstance(GirlsFragment.LayoutType.StaggeredGridLayout,"酒店推荐"));
                    break;
                case 4:
                    listTabs.add("行程推荐" );
                    listData.add(GirlsFragment.newInstance(GirlsFragment.LayoutType.StaggeredGridLayout,"行程推荐"));
                    break;

//
                default:

                    break;
            }

        }

        viewPagerFragmentAdapter = new ViewPagerFragmentAdapter(getSupportFragmentManager(),listData,listTabs);
        viewPager.setAdapter(viewPagerFragmentAdapter);

        tabLayout.setupWithViewPager(viewPager);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                if(listData.get(position) instanceof  OnScrollListener){
                    onScrollListener = (OnScrollListener) listData.get(position);
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    public void onEventMessage(EventMessage em) {
        superSlidingPaneLayout.openPane();
    }


    private void clickFab(){

        if(onScrollListener!=null){
            onScrollListener.onScroll();
        }

//        Uri uri = Uri.parse("mailto:jenly1314@gmail.com");
//        Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
//        startActivity(Intent.createChooser(intent, "Choose Email Client"));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.fab:
                clickFab();
                break;
            case R.id.card:
                SpfUtils.put(this, "islogin", false);
                Toast.makeText(this, "退出成功！", Toast.LENGTH_SHORT).show();
//                MyEvent myEvent=new MyEvent();
//                myEvent.setContent("finish");
//                EventBus.getDefault().post(myEvent);
                finish();

                startActivity(new Intent(PhotoActivity.this,Signin_Activity.class));
                break;
            case R.id.fankui:


                startActivity(new Intent(PhotoActivity.this,StudentManagerActivity.class));
                break;
    }
    }

    long exitTime=0;// 退出时间

    /** 捕捉按下返回键操作 */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO 按两次返回键退出应用程序
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            // 判断间隔时间 大于2秒就退出应用
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                // 应用名
                String applicationName = getResources().getString(
                        R.string.app_name);
                String msg = "再按一次返回键退出" + applicationName;
                //String msg1 = "再按一次返回键回到桌面";
                Toast.makeText(PhotoActivity.this, msg, Toast.LENGTH_LONG).show();
                // 计算两次返回键按下的时间差
                exitTime = System.currentTimeMillis();
            } else {
                // 关闭应用程序
                finish();
                // 返回桌面操作
                // Intent home = new Intent(Intent.ACTION_MAIN);
                // home.addCategory(Intent.CATEGORY_HOME);
                // startActivity(home);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
